﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class UpdateForm : System.Windows.Forms.Form
    {
        private string rootPath;
        
        private int projID;
        private string projVersion;

        public UpdateForm(String path)
        {
            InitializeComponent();

            rootPath = path;

            TxtReader.ReadDataFile(path);
            projID = Int32.Parse(TxtReader.GetString(TxtReader.LINE.PROJECTID));
            projVersion = TxtReader.GetString(TxtReader.LINE.VERSION);

            CCL.TableStructure.Project projInfo = MainForm.GetProjectInfo(projID);

            if(projInfo == null)
            {
                MessageBox.Show("알수 없는 오류가 발생했습니다.");
                this.Close();
            }

            lbProjectPath.Text += rootPath;
            lbProjectName.Text += CCL.Utility.StringUtility.ConvertToStr(projInfo.ProjectName);
            lbAdminID.Text += CCL.Utility.StringUtility.ConvertToStr(projInfo.ProjectOwner.Email) + " (" + CCL.Utility.StringUtility.ConvertToStr(projInfo.ProjectOwner.Name) + ")";
            lbVersion.Text += projVersion;
        }
        
        // 업데이트.. 아.. 자고시퍼..
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            CCL.SocketMessage.Request.UpdateProject packet = new CCL.SocketMessage.Request.UpdateProject(projID, MainForm.userData.UserID, Int32.Parse(projVersion));
            CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
            byte[] msg = message.GetByte();

            CCL.SocketMessage.Message updateMsg = MainForm.connector.Execute(msg);
            CCL.SocketMessage.Response.FileDownloadReady updateResponse = (CCL.SocketMessage.Response.FileDownloadReady)updateMsg.Body;

            if (updateResponse.Result == CCL.SocketMessage.Constant.Result.Success)
            {
                for (int i = 0; i < updateResponse.FileCount; i++)
                {
                    CCL.SocketMessage.Request.FileDownload downPacket = new CCL.SocketMessage.Request.FileDownload(updateResponse.PoolID);
                    message = CCL.SocketMessage.Message.MessageRequestBuilder(downPacket);
                    byte[] downMsg = message.GetByte();

                    CCL.SocketMessage.Message downloadMsg = MainForm.connector.Execute(downMsg);
                    CCL.SocketMessage.Response.FileDownload downloadResponse = (CCL.SocketMessage.Response.FileDownload)downloadMsg.Body;

                    CCL.Utility.FileUtility.BytesToFile(downloadResponse.FileBytes, rootPath + downloadResponse.FileName);

                    progressBar1.Value = ((i + 1) / updateResponse.FileCount) * 100;
                }

                MessageBox.Show("Complete Update");
                this.Close();
            }
            else
            {
                MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(updateResponse.ErrorMessage));
            }
        }
    }
}
